package com.qtummatrix.dao.impl;

import com.qtummatrix.dao.ProductDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
/**
 * create by Gongshiyong  2019-11-21 19:39
 */

@Repository
public class ProductDaoImpl implements ProductDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public List<Map<String, Object>> getProductList(int bankid,String loanName,int seriesid,int state,int pageNo,int pageSize) {
        String sql = "select p.id,p.loanName,b.bankname,series.serieName,state.productState,p.benefitsDown,p.benefitsUp,p.longestperiod,p.rateDown,p.rateUp from loanproductsentity p left join bankinfoentity b on p.bankid = b.id left join seriesentity series on p.seriesid = series.id left join stateentity state on p.state = state.id where 1=1 ";
        if(bankid != 0){
            sql+="and bankid=? ";
        }else{
            sql+="and (1=1 or bankid=?) ";
        }
        if(!loanName.equals("")){
            sql+="and loanName=? ";
        }else{
            sql+="and (1=1 or loanName=?) ";
        }
        if(seriesid != 0){
            sql+="and seriesid=? ";
        }else{
            sql+="and (1=1 or seriesid=?) ";
        }
        if(state != 0){
            sql+="and state=? ";
        }else{
            sql+="and (1=1 or state=?) ";
        }

        sql+="order by p.id limit "+(pageNo * pageSize) + ", "+pageSize;
        List<Map<String, Object>> productList = jdbcTemplate.queryForList(sql,bankid,loanName,seriesid,state);
        return productList;
    }

    public Integer getProductSize(int bankid,String loanName,int seriesid,int state) {
        String sql = "select count(*) from loanproductsentity ";
        if(bankid != 0){
            sql+="where bankid=? ";
        }else{
            sql+="where (1=1 or bankid=?) ";
        }
        if(!loanName.equals("")){
            sql+="and loanName=? ";
        }else{
            sql+="and (1=1 or loanName=?) ";
        }
        if(seriesid != 0){
            sql+="and seriesid=? ";
        }else{
            sql+="and (1=1 or seriesid=?) ";
        }
        if(state != 0){
            sql+="and state=? ";
        }else{
            sql+="and (1=1 or state=?) ";
        }
        Integer count = jdbcTemplate.queryForObject(sql, Integer.class,bankid,loanName,seriesid,state);
        return count;
    }

    public List<Map<String, Object>> getAllproductBankName() {
        String sql = "select id, bankname from BankinfoEntity";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        return list;
    }

    public List<Map<String, Object>> getAllproductSerie() {
        String sql = "select id, serieName from SeriesEntity";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        return list;
    }

    public List<Map<String, Object>> getAllproductState() {
        String sql = "select id, productState from StateEntity";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        return list;
    }

    public int deleteProduct(int id) {
        String sql = "delete from loanproductsentity where id=? ";
        int count = jdbcTemplate.update(sql,id);
        return count;
    }

    public int updateProductState(int id, int state) {
        String sql = "update loanproductsentity set state = ? where id = ?";
        int count = jdbcTemplate.update(sql,state,id);
        return count;
    }

    public List<Map<String, Object>> queryProductList(int productBank, String productName, int productSerie, int productState) {

        System.out.println(productBank);
        System.out.println(productName);
        System.out.println(productSerie);
        System.out.println(productState);
        String sql = "";
        List<Map<String, Object>> queryProductList = jdbcTemplate.queryForList(sql);
        return queryProductList;
    }

    public int addProduct(int bankid, String loanName, int seriesid, String definition, String customer, BigDecimal benefitsDown, BigDecimal benefitsUp, Integer shortestperiod, Integer longestperiod, BigDecimal rateDown, BigDecimal rateUp, String logo) {
        String sql = "insert into loanproductsentity values(null, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?,2)";
        int count = jdbcTemplate.update(sql,loanName,bankid,seriesid,definition,customer,shortestperiod,longestperiod,benefitsUp,benefitsDown,rateUp,rateDown,logo);
        return count;
    }

    public List<Map<String, Object>> getProductinfo(int id) {
        String sql = "select p.id,p.loanName,b.bankname,series.serieName,p.definition,p.customer,state.productState,p.benefitsDown,p.benefitsUp,p.shortestperiod,p.longestperiod,p.rateDown,p.rateUp,p.logo from loanproductsentity p left join bankinfoentity b on p.bankid = b.id left join seriesentity series on p.seriesid = series.id left join stateentity state on p.state = state.id where p.id="+id;
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        return  list;
    }

    public int updateProductinfo(int id, String definition, String customer) {
        String sql = "update loanproductsentity set definition = ? , customer = ? where id = ?";
        int count = jdbcTemplate.update(sql,definition,customer,id);
        return count;
    }

}
